package com.kgc.scd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kgc.scd.bean.ShopCoupon;
import com.kgc.scd.bean.ShopGoods;
import com.kgc.scd.bean.ShopUser;
import com.kgc.scd.bean.ShopUserCoupon;
import com.kgc.scd.mapper.*;
import com.kgc.scd.service.CouponService;

import com.kgc.scd.util.SnowUtil;
import com.kgc.scd.vo.ShopUserCouponVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;

/**
 * Created On : 2022/11/15.
 * <p>
 * Author : yantao
 * <p>
 * Description: CouponServiceImpl
 */
@Slf4j
@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    private ShopCategoryMapper shopCategoryMapper;

    @Autowired
    private ShopUserMapper shopUserMapper;

    @Autowired
    private SnowUtil snowUtil;

    @Autowired
    private ShopCoodsMapper shopCoodsMapper;

    @Autowired
    private ShopCouponMapper shopCouponMapper;

    @Autowired
    private ShopUserCouponMapper shopUserCouponMapper;

    /**
     * @author : yantao
     * @date   : 2022/11/15
     * @param  : [couponId, categoryId, userId]
     * @return : java.lang.String
     * @description : 添加用户的优惠卷
     */
    @Override
    public String addCouponsUser(String couponId, String userId) {
        log.info("###### 开始添加用户的优惠券:种类:{},用户:{}######",couponId,userId);
        //判断用户是否可以兑换优惠券
        ShopUser shopUser= shopUserMapper.selectById(userId);
        Integer i= shopUser.getIntegration();
        ShopCoupon shopCoupon = shopCouponMapper.selectById(couponId);
        Integer ci = shopCoupon.getNeedIntegration();

        //减库存
        if (shopCoupon.getNum()>=1){
            shopCoupon.setNum(shopCoupon.getNum()-1);
            shopCouponMapper.updateById(shopCoupon);
        }else {
            return null;
        }

        //减积分
        if (i - ci>=0){
            shopUser.setIntegration(i - ci);
            QueryWrapper<ShopUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",shopUser.getId());
            shopUserMapper.update(shopUser,queryWrapper);
        }else {
            return null;
        }



        ShopUserCoupon shopUserCoupon = new ShopUserCoupon();
        String id = snowUtil.snowId( );
        shopUserCoupon.setId(id);
        shopUserCoupon.setGetTime(new Date());
        shopUserCoupon.setCategoryId(shopCoupon.getCategoryId());
        shopUserCoupon.setCouponId(couponId);
        shopUserCoupon.setUserId(shopUser.getId());
        //进行添加用户的优惠券
        if (shopUserCouponMapper.insert(shopUserCoupon)>0){
            log.info("###### 结束添加用户的优惠券id:{}######",id);
            return id;
        }
        return null;
    }

    /**
     * @author : yantao
     * @date   : 2022/11/16
     * @param  : [userCouponId, categoryId, userId]
     * @return : java.lang.String
     * @description : 判断优惠券状态,返回信息
     */
    @Override
    public Map<String,Object> examineCouponsState(String userCouponId, String categoryId, Double money, String userId) {
        log.info("###### 开始查询用户的优惠券状态:id:{},商品种类:{},{}######",userCouponId,categoryId,userId);

        ShopUserCoupon shopUserCoupon = shopUserCouponMapper.selectById(userCouponId);

        Map<String,Object> reMap = new HashMap<>();
        ShopCoupon shopCoupon;

        try {
           shopCoupon= shopCouponMapper.selectById(shopUserCoupon.getCouponId());
        } catch (NullPointerException e){
            reMap.put("condition","没有优惠券该优惠券");
            reMap.put("money",money);
            reMap.put("state",false);
            return reMap;
        }

        Date expirationTime = shopCoupon.getExpirationTime();
        if (expirationTime.before(new Date())){

            reMap.put("condition","该优惠券已经过期");
            reMap.put("money",money);
            reMap.put("state",false);
            return reMap;
        }

        if (!ObjectUtils.isEmpty(shopUserCoupon.getUseTime())){
            reMap.put("condition","已经使用");
            reMap.put("money",money);
            reMap.put("state",false);
            return reMap;
        }

        if (shopUserCoupon.getCategoryId()==null){

        }else
            if (!shopUserCoupon.getCategoryId().equals(categoryId)){
            reMap.put("condition","该商品不支持该优惠券");
            reMap.put("money",money);
            reMap.put("state",false);
            return  reMap;
        }



        if (shopCoupon.getMinGoodsAmount()>money){
            reMap.put("condition","该商品价格未满足优惠券");
            reMap.put("money",money);
            reMap.put("state",false);
            return  reMap;
        }

        log.info("###### 结束判断用户的优惠券状态 ,可以使用######");
        reMap.put("condition","优惠券可以使用");
        reMap.put("money",money-shopCoupon.getTypeMoney());
        reMap.put("state",true);

        return reMap;
    }

    /**
     * @author : yantao
     * @date   : 2022/11/16
     * @param  : [userCouponId]
     * @return : java.lang.String
     * @description : 删除优惠券,true成功,false删除失败
     */
    @Override
    public boolean deleteUserCoupon(String userCouponId) {
        log.info("###### 开始删除用户的优惠券状态:id:{}######",userCouponId);
        if (shopUserCouponMapper.selectById(userCouponId).getUseTime() == null){
            return false;
        }

        if (shopUserCouponMapper.deleteById(userCouponId)>0){
            return true;
        }
        return false;
    }

    /**
     * @author : yantao
     * @date   : 2022/11/16
     * @param  : [userId]
     * @return : java.util.List<com.kgc.scd.vo.ShopUserCouponVO>
     * @description : 查询所有用户的优惠券,所包含状态
     */
    @Override
    public List<ShopUserCouponVO> selectUserCouponAll(String userId) {
        log.info("###### 开始查询所有用户的优惠券状态:id:{}######",userId);
        QueryWrapper<ShopUserCoupon> shopUserCouponQueryWrapper = new QueryWrapper<>();

        shopUserCouponQueryWrapper.eq("user_id",userId);
        shopUserCouponQueryWrapper.orderByDesc("get_time");
        List<ShopUserCoupon> shopUserCouponList = shopUserCouponMapper.selectList(shopUserCouponQueryWrapper);

        List<ShopUserCouponVO> shopUserCouponVOList = new ArrayList<>();

        //装载VO模型数据
        shopUserCouponList.forEach(a->{
            ShopCoupon shopCoupon = shopCouponMapper.selectById(a.getCouponId());
            ShopUserCouponVO shopUserCouponVO = new ShopUserCouponVO();



            Date expirationTime = shopCoupon.getExpirationTime();
            if (expirationTime.before(new Date())){

                shopUserCouponVO.setState("已过期");
            } else if (!ObjectUtils.isEmpty(a.getUseTime())){
                shopUserCouponVO.setState("已使用");
            }else {
                shopUserCouponVO.setState("未使用");
            }
            shopUserCouponVO.setCoupon(shopCoupon.getName());

            shopUserCouponVO.setOverTime(expirationTime);

            if (a.getCategoryId() == null){
                shopUserCouponVO.setCategoryId("所有商品");
            }else {
                shopUserCouponVO.setCategoryId(shopCategoryMapper.selectById(a.getCategoryId()).getName());
            }

            shopUserCouponVO.setId(a.getId());


            shopUserCouponVOList.add(shopUserCouponVO);

        });
        return shopUserCouponVOList;
    }

    /**
     * @author : yantao
     * @date   : 2022/11/16
     * @param  : [userCouponId]
     * @return : void
     * @description : 使用优惠券
     */
    @Override
    public void useCoupon(String userCouponId) {

        ShopUserCoupon shopUserCoupon = new ShopUserCoupon();
        shopUserCoupon.setUseTime(new Date());
        shopUserCoupon.setId(userCouponId);

        shopUserCouponMapper.updateById(shopUserCoupon);
    }


}
